<?php
if (is_object($your_recent_history)) {
if (
        (($your_recent_history->count_viewed() > 0) || ($your_recent_history->count_searches() > 0) || ($your_recent_history->count_viewed_cats() > 0))   // we have content
    and ($your_recent_history->on)                          // we want it
    and ($request_type != 'SSL')               // we are not secure
   ) { // displaying

  //*******************************************************************************
  DEFINE('SHOW_ADD', true);  // show the extra product
  DEFINE('SHOW_AP', true);   // show also purchased product
  DEFINE('AP_PRE', false);   // use ap preselection query
  //*******************************************************************************

require(DIR_WS_LANGUAGES . $language . '/' . FILENAME_YOUR_RECENT_HISTORY);
// initialize variable
$yrh='';
// start output buffer
ob_start();

  echo '<table border="0" width="100%" cellspacing="1" cellpadding="0">
         <tr>
          <td>
           <table border="0" width="100%" cellpadding="0" align="center">
            <tr>
             <td>
              <table style="border: 1px solid gray" width="100%" align="center" cellpadding="0">
               <tr>
                <td>
                 <table border="0" width="100%" cellpadding="4" >';
  echo '<tr>
         <td nowrap colspan="7" class="pageHeading">' . YOUR_RECENT_HISTORY . '</td>
         <td class="boxtext" align="right"><a href="' . tep_href_link(basename($PHP_SELF), tep_get_all_get_params(array('action','lng','currency')) . 'action=vr'). '"><img src="images/erase.gif" border="0" alt="' . ERASE_MY_RECENT_HISTORY . '" title="' . ERASE_MY_RECENT_HISTORY . '"></a>&nbsp;<a href="' . tep_href_link(basename($PHP_SELF), tep_get_all_get_params(array('action','lng','currency')) . 'action=vs'). '"><img src="images/erase2.gif" border="0" alt="' . DO_NOT_SHOW_ME_AGAIN . '" title="' . DO_NOT_SHOW_ME_AGAIN . '"></a></td>
        </tr><tr>';


  $row = 0;

// PRODUCTS DISPLAY START

  /* get the products array from the class containing all products */
  $items  = $your_recent_history->get_viewed_items();
  $n=sizeof($items);
  $index = 1;

  /* fetch the info for products */
  for ($i=0; $i<$n; $i++) {
    $viewed_query = tep_db_query("select pd.products_name, p.products_image
                                  from " . TABLE_PRODUCTS_DESCRIPTION . " pd,
                                       " . TABLE_PRODUCTS . " p
                                  where p.products_id = '" . $items[$i] . "' and 
                                        pd.products_id = p.products_id and
                                        p.products_status = 1 and
                                        pd.language_id = '" . $languages_id . "'");

        if ($viewed_info = tep_db_fetch_array($viewed_query)) {
          $items_on_display[$i] = array('id' => $items[$i],
                                        'image' => $viewed_info['products_image'],
                                        'name'  => $viewed_info['products_name']);
        } else {
          // in case a product is recorded which is not active, set the image in language file
          $items_on_display[$i] = array('id' => $items[$i],
                                        'image' => UNAVAILABLE_IMAGE,
                                        'name'  => UNAVAILABLE);
        }
    }
 
  $col = 0;
  $pwidth = 25;
  echo '<td>&nbsp;</td><td class="tableHeading" width="'.$pwidth.'%" valign="top" align="left">';
  if ($n > 0) {
    $col++;
    echo '<a href="' . tep_href_link(basename($PHP_SELF), tep_get_all_get_params(array('action','lng','currency')) . 'action=cp'). '"><img src="images/erase.gif" border="0" alt="' . ERASE_MY_RECENT_HISTORY . _OF_PRODUCTS . '" title="' . ERASE_MY_RECENT_HISTORY . _OF_PRODUCTS . '"></a>&nbsp;'.VIEWED.PRODUCTS .'<br>' . tep_draw_separator('pixel_trans.gif', '100%', 4);
    echo '<table cellpadding="2">';
    for ($i=0; $i<$n; $i++) {//DIR_WS_IMAGES 
      echo '<tr height="40px"><td valign="top" align="center">' . '<a href="' . tep_href_link(FILENAME_PRODUCT_INFO, 'products_id=' . $items_on_display[$i]['id']) . '">' . tep_image($items_on_display[$i]['image'],$items_on_display[$i]['name'], SMALL_IMAGE_WIDTH, SMALL_IMAGE_HEIGHT) . '</a>';
      echo '</td><td class="main" valign="top">';
      echo '<a href="' . tep_href_link(FILENAME_PRODUCT_INFO, 'products_id=' . $items_on_display[$i]['id']) . '">' . $items_on_display[$i]['name'] . '</a>';
      echo '</td></tr>';
    }
    echo '</table>';
  }
  echo '</td>';

// PRODUCTS DISPLY END
// CATEGORIES DISPLAY START

  $cats  = $your_recent_history->get_viewed_cats();
  $m=sizeof($cats);
  $index = 1;
  for ($i=0; $i<$m; $i++) {
    $mcPath_array = tep_parse_category_path($cats[$i]);
    $mcPath = implode('_', $mcPath_array);
    $v_category_id = $mcPath_array[(sizeof($mcPath_array)-1)];
    $category_query = tep_db_query("select cd.categories_name, c.parent_id from " . TABLE_CATEGORIES . " c, " . TABLE_CATEGORIES_DESCRIPTION . " cd where c.categories_id = '" . (int)$v_category_id  . "' and cd.categories_id = '" . (int)$v_category_id  . "' and cd.language_id = '" . (int)$languages_id . "'");
    $category = tep_db_fetch_array($category_query);
    $parent_id = $category['parent_id'];
    if ($parent_id > 0) {
      $category_parent_query = tep_db_query("select categories_name from " . TABLE_CATEGORIES_DESCRIPTION . " where categories_id = '" . (int)$category['parent_id'] . "' and language_id = '" . (int)$languages_id . "'");
      $category_parent = tep_db_fetch_array($category_parent_query);
      $parent_name = $category_parent['categories_name'];
      $sep = ' ~ ';
    } else {
      $parent_name = '';
      $sep = '';
    }
    $cats_on_display[$i] = array('id' => $cats[$i],'name' => $parent_name . $sep . $category[categories_name]);
  }
  $cwidth = 25;
  echo '<td>&nbsp;</td><td class="tableHeading" width="'.$cwidth.'%" valign="top" align="left">';
  if ($m > 0) {
    $col++;
    echo '<a href="' . tep_href_link(basename($PHP_SELF), tep_get_all_get_params(array('action','lng','currency')) . 'action=cc'). '"><img src="images/erase.gif" border="0" alt="' . ERASE_MY_RECENT_HISTORY . _OF_CATEGORIES . '" title="' . ERASE_MY_RECENT_HISTORY . _OF_CATEGORIES . '"></a>&nbsp;'.VIEWED.CATEGORIES . '<br>' . tep_draw_separator('pixel_trans.gif', '100%', 4);
    echo '<table cellpadding="2" cellspacing="0">';
    for ($i=0; $i<$m; $i++) {
      echo '<tr><td valign="middle">' . tep_image(DIR_WS_IMAGES . 'ico_arrow.gif', CATEGORY);
      echo '</td><td class="main">';
      echo '<a href="' . tep_href_link(FILENAME_DEFAULT, 'cPath=' . $cats_on_display[$i]['id']) . '">' . $cats_on_display[$i]['name'] . '</a>';
      echo '</td></tr>';
    }
    echo '</table>';
  }
  echo '</td>';

// CATEGORIES DISPLAY END
// SEARCHES DISPLY START

  $searches = $your_recent_history->get_searches();
  $ss=sizeof($searches);
  echo '<td>&nbsp;</td><td class="tableHeading" width="25%" valign="top" align="left">';
  if ($ss > 0) {
    $col++;
    echo '<a href="' . tep_href_link(basename($PHP_SELF), tep_get_all_get_params(array('action','lng','currency')) . 'action=cs'). '"><img src="images/erase.gif" border="0" alt="' . ERASE_MY_RECENT_HISTORY . _OF_SEARCHES . '" title="' . ERASE_MY_RECENT_HISTORY . _OF_SEARCHES . '"></a>&nbsp;'.SUCCESSFUL.SEARCHES . '<br>' . tep_draw_separator('pixel_trans.gif', '100%', 4);
    echo '<table cellpadding="2" cellspacing="0">';
    for ($i=0; $i<$ss; $i++) {
      echo '<tr><td valign="middle">' . tep_image(DIR_WS_IMAGES . 'search_icon_s.gif', SEARCH);
      echo '</td><td class="main">';
      echo '<a href="' . tep_href_link(FILENAME_ADVANCED_SEARCH_RESULT, 'keywords=' . $searches[$i] .'&inc_subcat=1&search_in_description=1') . '">' . $searches[$i] . '</a>';
      echo '</td></tr>';
    }
    echo '</table>';
  }
echo '</td>';

// SEARCHES DISPLAY END

/* try to find also purchased products based on all products in the history */
/* any other than currently on the product info page                        */

if (SHOW_AP) {
  $mtm = rand();
  // Also Purchased query based on Also Purchased Preselection
  if (AP_PRE) {
    # First let's get all the orders for this product
    $ap_query = tep_db_query("select ap.ap_id, 
                                     p.products_image,
                                     p.products_price,
                                     p.products_tax_class_id,
                                     pd.products_name
                              from ap_preselection ap, 
                                   products_description pd, 
                                   products p 
                              where ap.products_id in ('" . implode("','", $items) . "')
                                and p.products_id = ap.ap_id 
                                and pd.products_id = ap.ap_id 
                                and p.products_id != '" . $HTTP_GET_VARS['products_id'] . "' 
                                and pd.language_id = '" . $languages_id . "' 
                                and p.products_quantity >= 0 
                                and p.products_status = '1' 
                              order by rand($mtm) 
                              limit 1 ");
  } else {
    // The original also purchased query, use this if you have no ap preselection and hope for the best (performance-wise)
    $ap_query = tep_db_query("select p.products_id,
                                     p.products_price,
                                     p.products_tax_class_id,
                                     p.products_image,
                                     pd.products_name
                              from " . TABLE_ORDERS_PRODUCTS . " opa,
                                   " . TABLE_ORDERS_PRODUCTS . " opb,
                                   " . TABLE_ORDERS . " o,
                                   " . TABLE_PRODUCTS . " p,
                                   " . TABLE_PRODUCTS_DESCRIPTION . " pd
                              where opa.products_id in ('" . implode("','", $items) . "')
                                and opa.orders_id = opb.orders_id
                                and opb.products_id != '" . (int)$HTTP_GET_VARS['products_id'] . "'
                                and opb.products_id = p.products_id
                                and opb.orders_id = o.orders_id
                                and pd.products_id = p.products_id
                                and pd.language_id = '" . $languages_id . "'
                                and p.products_status = '1'
                              group by p.products_id
                              order by rand($mtm)
                              limit 1 ");
  }
  /* if we find results, display the also purchased product */
  $ap_found = false;
  $fwidth = ceil(100/($col+1));
  if ($ap = tep_db_fetch_array($ap_query)) {
    $ap_found = true;
    $ap['special_price'] = tep_get_products_special_price($ap['products_id']);
    if (tep_not_null($ap['special_price'])) {
      $ap_price = '<s>' . $currencies->display_price($ap['products_price'], tep_get_tax_rate($ap['products_tax_class_id'])) . '</s><br>';
      $ap_price .= '<span class="productSpecialPrice">' . $currencies->display_price($ap['special_price'], tep_get_tax_rate($ap['products_tax_class_id'])) . '</span>';
    } else {
      $ap_price = $currencies->display_price($ap['products_price'], tep_get_tax_rate($ap['products_tax_class_id']));
    }
    
    echo '<td width="'.$fwidth.'%" colspan="2" align="right" valign="middle">
           <table width="100%" align="center" border="0">
            <tr>
             <td align="center" class="main">';
    echo CUSTOMERS_HISTORY_ALSO_PURCHASED . '<br>';
    echo '<a href="' . tep_href_link(FILENAME_PRODUCT_INFO, 'products_id=' . $orders['ap_id']) . '">' . tep_image($ap['products_image'], $ap['products_name'], SMALL_IMAGE_WIDTH, SMALL_IMAGE_HEIGHT) . '<br>' . $ap['products_name'] . '</a>' .
         '<br>' . $ap_price;
    echo '   </td>
            </tr>
           </table>
          </td>';
  }
}
  /* if there are no also purchased results we display a random product */
  /* any other than in the history or on the product info page          */
if (((!SHOW_AP) && (SHOW_ADD)) || ((SHOW_AP) && (SHOW_ADD) && (!$ap_found)))  {
    $mtm= rand();
    $add_query = 
    tep_db_query("select p.products_id,
                         pd.products_name, 
                         p.products_image, 
                         p.products_tax_class_id,
                         p.products_price
                  from " . TABLE_PRODUCTS . " p, 
                       " . TABLE_PRODUCTS_DESCRIPTION . " pd 
                  where p.products_id = pd.products_id 
                    and p.products_id not in ('" . implode("','", $items) . "')
                    and p.products_id != '" . $HTTP_GET_VARS['products_id'] . "' 
                    and p.products_status = 1 
                    and pd.language_id ='" .  (int)$languages_id . "' 
                  order by rand($mtm) limit 1");

        if ($add_info = tep_db_fetch_array($add_query)) {
          $add_info['special_price'] = tep_get_products_special_price($add_info['products_id']);

          if (tep_not_null($add_info['special_price'])) {
            $add_price = '<s>' . $currencies->display_price($add_info['products_price'], tep_get_tax_rate($add_info['products_tax_class_id'])) . '</s><br>';
            $add_price .= '<span class="productSpecialPrice">' . $currencies->display_price($add_info['special_price'], tep_get_tax_rate($add_info['products_tax_class_id'])) . '</span>';
          } else {
            $add_price = $currencies->display_price($add_info['products_price'], tep_get_tax_rate($add_info['products_tax_class_id']));
          }

          echo '<td width="'.$fwidth.'%" colspan="2" valign="middle" align="right">
                 <table align="center" border="0">
                  <tr>
                   <td align="center" class="main">';
          echo YOU_MAY_ALSO_APPRECIATE . '<br>'; //DIR_WS_IMAGES .s
          echo '<a href="' . tep_href_link(FILENAME_PRODUCT_INFO, 'products_id=' . $add_info['products_id']) . '">' . tep_image( $add_info['products_image'], $add_info['products_name'], SMALL_IMAGE_WIDTH, SMALL_IMAGE_HEIGHT) . '<br>' . $add_info['products_name'] . '</a>' .
               '<br>' . $add_price;
          echo '   </td>
                  </tr>
                 </table>
                </td>';
        }
}

echo '          </tr>
               </table>
              </td>
             </tr>
            </table>
           </td>
          </tr>
         </table>
        </td>
       </tr>
      </table>';
// put output buffer in the variable for later use
$yrh = ob_get_clean();
} // Displaying
} // object check
?>
